package com.trace.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
//@ComponentScan(basePackages = {"com.trace.demo", "com.trace.demo.repository", "com.trace.demo.controller", "com.trace.demo.entity"})
public class TraceApplication {

    public static void main(String[] args) {
        SpringApplication.run(TraceApplication.class, args);
    }

    @Bean
    // 创建一个WebMvcConfigurer对象，用于配置跨域请求
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            // 重写addCorsMappings方法，用于配置跨域请求的规则
            public void addCorsMappings(CorsRegistry registry) {
                // 配置允许跨域的路径
                registry.addMapping("/**")
                        // 配置允许的请求来源
                        .allowedOriginPatterns("*")
                        // 配置允许的请求方法
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                        // 配置允许的请求头
                        .allowedHeaders("*")
                        // 配置是否允许发送Cookie
                        .allowCredentials(true)
                        // 配置预检请求的有效期
                        .maxAge(3600);
            }
        };
    }
}
